class Node():
    def __init__(self, data):
        self.data = data
        self.next = None
        self.pre = None

class CircularDoublyLinkedList():
    def __init__(self):#对头节点进行初始化
        self.head = Node(0)
        self.size=0
        self.head.next = self.head
        self.head.pre = self.head

    def head_insert(self, data):#头插法
        new_node = Node(data)
        new_node.next = self.head.next
        new_node.pre = self.head
        self.head.next.pre = new_node
        self.head.next = new_node
        self.size += 1

    def tail_insert(self,data):#尾插法注意指针的顺序
        new_node=Node(data)
        new_node.next=self.head
        self.head.pre.next=new_node
        new_node.pre=self.head.pre
        self.head.pre=new_node
        self.size+=1

    def delete_list(self,data):#删除
        current_node=self.head.next
        while current_node!=self.head:
            if(current_node.data==data):
                current_node.next.pre=current_node.pre
                current_node.pre.next=current_node.next
                self.size-=1
                return True
            current_node=current_node.next
        return False

    def print_list(self):#遍历
        current = self.head.next
        while current != self.head:
            print(current.data)
            current = current.next

if __name__ == "__main__":
    cdll = CircularDoublyLinkedList()
    cdll.head_insert(2)
    cdll.head_insert(1)
    cdll.tail_insert(3)
    cdll.tail_insert(4)
    cdll.delete_list(3)
    cdll.print_list()